package com.zm.exam.springbootexam.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zm.exam.springbootexam.entry.User;
import com.zm.exam.springbootexam.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserBiz {
    @Autowired
    UserMapper userMapper;
    public void reg(User user) throws BizException {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName,user.getName());
        if (userMapper.exists(lqw)){
            throw new BizException("该账号已注册！","name");
        }

        lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,user.getPhone());
        if (userMapper.exists(lqw)){
            throw new BizException("该手机号已注册！","phone");
        }
        userMapper.insert(user);
    }

    public User login(User user) throws BizException {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName,user.getName());
        lqw.eq(User::getPwd,user.getPwd());
        User dbUser = userMapper.selectOne(lqw);
        if (dbUser==null){
            throw new BizException("用户名或者密码错误！","name");
        }
        return dbUser;
    }
}
